let id = 0;

class Dep {
  constructor() {
    this.id = id++;
    this.subs = [];
  }
  // 收集依赖(视图中用了几次，收集几次watcher)
  depend() {
    // 希望watcher中存放dep
    Dep.target.addDep(this);
  }
  addSub(watcher) {
    this.subs.push(watcher);
  }
  // 更新watcher
  notify() {
    this.subs.forEach(watcher => {
      // update方法更新视图
      watcher.update();
    })
  }
}

Dep.target = null;
let stack = [];
// 添加watcher
export function pushTarget(watcher) {
  Dep.target = watcher;
  stack.push(watcher);
}
// 取消watcher
export function popTarget() {
  // Dep.target = null;
  stack.pop();
  Dep.target = stack[stack.length - 1];
}

export default Dep;